import { Model } from '@/utils/dva-helper'
import { getAllCitysPermission, getAllCitys, getCompany } from '@/service/common/index'
import { modelSeriesFun, translateDataToTree } from '@/utils/common/index'
import { getCarTypeList } from '@/service/carMessage/list'
import {
	getMainCompanyList,
	getRelevanceShop,
} from '@/service/carRewardRule/rewardRule'
// import { getResponse } from "@/utils/common/getResponse"

// 初始化Model层
export class DriverRewardRules {
	rewardInfoFrom: any = {}

	MaterialObjectForm: any = {}

	RentFreeForm: any = {}

	DeferredRentForm: any = {}

	activityTypeList: string[] = [
		'rewardInfoFrom',
		'MaterialObjectForm',
		'RentFreeForm',
		'DeferredRentForm',
	]

	companyData: any = []

	_companyData: any = []

	cityData: any = []

	carTypeList: any = []

	MainCompany: any = []

	shopRelevance: any = {} // 商品数据

	outTableList: any = []

	rewardDetailsList: any = [] // 奖励信息明细

	MaterialList: any = []

	RentFreeList: any = []

	MAXTERM: any = null

	basic: any = {}

	basicCopy: any = {}

	ruleParamsPlan: any = {}

	detailRecord: any = []

	otherName: any = {}
}

const model: Model<DriverRewardRules> = {
	namespace: 'driverRewardRules',
	state: new DriverRewardRules(),
	// 订阅
	subscriptions: {},
	effects: {
		/**
		 * 获取城市信息
		 * @param param0
		 * @param param1
		 */
		*getAllPermission({ payload }, { call, put }) {
			const res = yield call(getAllCitysPermission, payload)
			if (res) {
				yield put({
					type: 'updateState',
					payload: {
						cityData: res?.data,
					},
				})
			}
		},
		/**
		 * 获取城市信息
		 * @param param0
		 * @param param1
		 */
		*getAllCitys({ payload }, { call, put }) {
			const res = yield call(getAllCitys, payload)
			if (res) {
				yield put({
					type: 'updateState',
					payload: {
						cityData: res?.data,
					},
				})
			}
		},

		/**
		 * 获取公司信息
		 * @param param0
		 * @param param1
		 */
		*getCompany({ payload }, { call, put }) {
			const res = yield call(getCompany, payload)
			if (res) {
				const data = res?.data
				res?.data?.forEach(
					(
						item: { disabled: boolean; type: any },
						index: number,
					) => {
						if (item.type !== 0) {
							data[index].disabled = true
						}
					},
				)
				const list = translateDataToTree(data)
				yield put({
					type: 'updateState',
					payload: {
						companyData: list,
						_companyData: res?.data,
					},
				})
			}
		},
		/**
		 * 获取品牌车型车系
		 * @param param0
		 * @param param1
		 */
		*getCarTypeList({ payload }, { call, put }) {
			const res = yield call(getCarTypeList, payload)
			if (res) {
				yield put({
					type: 'updateState',
					payload: {
						carTypeList: modelSeriesFun(res?.data),
					},
				})
			}
		},
		/**
		 * 获取主体公司
		 * @param param0
		 * @param param1
		 */
		*getMainCompanyList({ payload }, { call, put }) {
			const res = yield call(getMainCompanyList, payload)
			if (res) {
				yield put({
					type: 'updateState',
					payload: {
						MainCompany: res?.data,
					},
				})
			}
		},
		/**
		 * 查询新增的关联的商品数据
		 * @param param0
		 * @param param1
		 */
		*getRelevanceShop({ payload, callback }: any, { call, put }) {
			const res = yield call(getRelevanceShop, payload)
			if (res?.success) {
				yield put({
					type: 'updateState',
					payload: {
						shopRelevance: res?.data,
					},
				})
			}
			if (callback) callback(res)
		},
	},
	reducers: {
		updateState(state, { payload }) {
			return { ...state, ...payload }
		},
	},
}
export default model
